package com.alibaba.algo.controller.user;

import com.alibaba.algo.annotation.OperateLogAnnotation;
import com.alibaba.algo.apiimpl.user.SysCarouselServiceImpl;
import com.alibaba.algo.constants.Constants;
import com.alibaba.algo.cooperation.common.BaseResult;
import com.alibaba.algo.cooperation.feign.vo.req.SysCarouselRequestVO;
import com.alibaba.algo.cooperation.feign.vo.req.SysFileRequestVO;
import com.alibaba.algo.cooperation.feign.vo.res.SysCarouselResponseVO;
import com.alibaba.algo.cooperation.usercenter.GlobalResultStatus;
import com.alibaba.algo.enums.OperateTypeEnum;
import com.alibaba.algo.enums.ResourceTypeEnum;
import com.alibaba.algo.utils.FileUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Base64;

/**
 * 类SysCarouselController.java的实现描述：用户系统轮播图表 Controller
 *
 */
@Slf4j
@RestController
@RequestMapping(value = "/usercenter-admin-app/admin/sysCarousel")
public class SysCarouselController {

    @Autowired
    private SysCarouselServiceImpl sysCarouselService;

    /**
     * 更新系统轮播图配置信息
     * @param vo
     * @return
     */
    @PutMapping()
    @OperateLogAnnotation(resouceType = ResourceTypeEnum.CAROUSEL,operateType = OperateTypeEnum.UPDATE)
    public BaseResult saveOrUpdateCarousel(@RequestBody SysCarouselRequestVO vo) {
        if (ObjectUtils.isEmpty(vo)) {
            return BaseResult.buildError(GlobalResultStatus.PARAM_ERROR.getCode(), GlobalResultStatus.PARAM_ERROR.getMsg());
        }
        if(ObjectUtils.isEmpty(vo.getId())){
            //新增操作,业务逻辑上不会执行，因为有初始化数据
            //return sysCarouselService.insert(vo);
            return BaseResult.buildError(GlobalResultStatus.PARAM_ERROR.getCode(), "id不能为空");
        }else{//更新操作
            return sysCarouselService.update(vo);
        }
    }

    /**
     * 上传图片文件至数据库
     * @param fileType
     * @param fileName
     * @param fileSuffix
     * @param file
     * @return
     */
    @PostMapping("/sysFile")
    @OperateLogAnnotation(resouceType = ResourceTypeEnum.CAROUSEL,operateType = OperateTypeEnum.INSERT)
    public BaseResult uploadSysFile(String fileName,String fileSuffix,MultipartFile file,Integer fileType) {
        boolean allowSize = FileUtils.checkFileSize(file.getSize());
        if(!allowSize){
            log.error("SysCarouselController sysFile file size is more than 5M");
            return BaseResult.buildError(GlobalResultStatus.PARAM_ERROR.getCode(), "file size is more than 5M");
        }
        SysFileRequestVO sysFileRequestVO = new SysFileRequestVO();
        sysFileRequestVO.setFileName(fileName);
        sysFileRequestVO.setFileSuffix(fileSuffix);
        sysFileRequestVO.setFileType(fileType);
        String fileBase64 = null;
        try {
            switch(fileType){
                case Constants.SYS_FILE_TYPE_LOGO:
                   fileBase64 = Base64.getEncoder().encodeToString(file.getBytes());
                   break;
                case Constants.SYS_FILE_TYPE_CAROUSEL:
                default:
                    fileBase64 = Base64.getEncoder().encodeToString(FileUtils.compressFile(file));
                    break;
            }
        } catch (IOException e) {
            log.error("SysCarouselController uploadSysFile() file.getBytes() error:{}",e);
        }
        sysFileRequestVO.setFileBase64(fileBase64);
        return sysCarouselService.uploadFile(sysFileRequestVO);
    }

    /**
     * 系统配置页面获取登录页面的配置信息，或配置页面编辑时获取当前已配置过的信息
     * @return
     */
    @GetMapping()
    public BaseResult<SysCarouselResponseVO> getSysCarousel() {
        SysCarouselResponseVO sysCarouselResponseVO =  sysCarouselService.selectOne();
        //验证操作
        //String fileBase64 = sysCarouselResponseVO.getCarouselList().get(0).getFileBase64();
        //String fileName = sysCarouselResponseVO.getCarouselList().get(0).getFileName();
        //String fileSuffix = sysCarouselResponseVO.getCarouselList().get(0).getFileSuffix();
        //FileUtils.downloadAndCheckImg(fileBase64,fileName,fileSuffix);
        return BaseResult.buildSuccess(sysCarouselResponseVO);
    }
}
